//
//  PayCell.m
//  PigMarket
//
//  Created by mac on 2019/4/9.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PayCell.h"

@interface PayCell ()
/**
 title
 */
@property (nonatomic , strong) UILabel *title;
/**
 icon
 */
@property (nonatomic , strong) UIImageView *icon;
/**
 btn
 */
@property (nonatomic , strong) UIImageView *selectImg;

/**
 充值按钮
 */
@property (nonatomic , strong) UIButton *topUpBtn;
@end
@implementation PayCell

#pragma mark - init method
+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    PayCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[PayCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.title];
    [self.contentView addSubview:self.icon];
    [self.contentView addSubview:self.selectImg];
    [self.contentView addSubview:self.textField];
    [self.contentView addSubview:self.topUpBtn];
}

- (void)initLayout {
    // 布局子控件
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.icon.mas_right).offset(15);
        make.centerY.mas_equalTo(self.icon.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    [self.selectImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.icon.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    [self.topUpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.icon.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(56, 26));
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(64);
        make.centerY.mas_equalTo(self.title.mas_centerY);
        make.right.mas_equalTo(self.topUpBtn.mas_left).offset(-15);
    }];
    
}
#pragma mark - target aciton
- (void)topUpTapped {
    if (self.topUpBlock) {
        self.topUpBlock();
    }
}
#pragma mark - getter and setter
- (void)setInfoDic:(NSDictionary *)infoDic {
    self.icon.image = [UIImage imageNamed:infoDic[@"icon"]];
    self.title.text = infoDic[@"title"];

    if (([infoDic[@"icon"] length] > 0)) {//存在icon
        self.icon.hidden = NO;
        self.selectImg.hidden = NO;
    }else {
        self.icon.hidden = YES;
        self.selectImg.hidden = YES;

        if (![infoDic[@"title"] isEqualToString:@"￥"] && ![infoDic[@"title"] isEqualToString:@"可用余额"]) {
            [self.title mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(15);
            }];
        }
    }
    if (![infoDic[@"title"] isEqualToString:@"￥"]) {//除去输入金额输入框
        self.textField.hidden = YES;
        self.topUpBtn.hidden = YES;
    }
    if ([infoDic[@"title"] rangeOfString:@"可用余额"].location != NSNotFound) {
        self.title.textColor = [UIColor colorWithHexString:@"#999999"];
        self.title.font = [UIFont smallFont];
    }
    if ([infoDic[@"isSelect"] isEqualToString:@"NO"]) {
        self.selectImg.image = [UIImage imageNamed:@"pay_default"];
        
    } else if ([infoDic[@"isSelect"] isEqualToString:@"YES"]) {
        self.selectImg.image = [UIImage imageNamed:@"pay_default_selected"];
    }
}
- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.textColor = [UIColor textBlackColor];
        _title.font = [UIFont systemFontOfSize:15];
    }
    return _title;
}
- (UIImageView *)icon {
    if (!_icon) {
        _icon = [[UIImageView alloc] init];
        
    }
    return _icon;
}
- (UIImageView *)selectImg {
    if (!_selectImg) {
        _selectImg = [[UIImageView alloc] init];
     }
    return _selectImg;
}
- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.placeholder = @"请输入整数倍充值金额";
        _textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _textField;
}


- (UIButton *)topUpBtn {
    if (!_topUpBtn) {
        _topUpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_topUpBtn setTitleColor:[UIColor primaryColor] forState:UIControlStateNormal];
        _topUpBtn.titleLabel.font = [UIFont smallFont];
        [_topUpBtn setTitle:@"去充值" forState:UIControlStateNormal];
        _topUpBtn.layer.cornerRadius = 3;
        _topUpBtn.layer.masksToBounds = YES;
        _topUpBtn.layer.borderColor = [UIColor primaryColor].CGColor;
        _topUpBtn.layer.borderWidth = 1;
        [_topUpBtn addTarget:self action:@selector(topUpTapped) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _topUpBtn;
}

@end
